package com.crm.miaohe.utils;

import com.google.gson.Gson;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CustomK3CloudApi extends K3CloudApi {

    public CustomK3CloudApi() {
        super((String)null);
    }

    public CustomK3CloudApi(String serverUrl) {
        super(serverUrl, 120);
    }

    public CustomK3CloudApi(String serverUrl, int timeout) {
        super(serverUrl, timeout);
    }

    Method[] getMethodsByFields(Method[] pes, String pre, String[] fields) {
        Method[] rets = new Method[fields.length];

        for(int i = 0; i < fields.length; ++i) {
            for(int j = 0; j < pes.length; ++j) {
                if (pes[j].getName().toLowerCase().equals(pre + fields[i].toLowerCase())) {
                    rets[i] = pes[j];
                }
            }
        }

        return rets;
    }

    Method getMethodFromT(Method[] pes, String pname) {
        for(int i = 0; i < pes.length; ++i) {
            if (pes[i].getName().toLowerCase().equals(pname.toLowerCase())) {
                return pes[i];
            }
        }

        return null;
    }

    Object convertToDest(Type type, Object val) {
        if (val == null) {
            return null;
        } else if (type.getTypeName().equals(String.class.getTypeName())) {
            return val.toString();
        } else if (!type.getTypeName().equals(Integer.TYPE.getTypeName()) && !type.getTypeName().equals(Short.TYPE.getTypeName()) && !type.getTypeName().equals(Long.TYPE.getTypeName())) {
            if (type.getTypeName().equals(BigDecimal.class.getTypeName())) {
                return new BigDecimal(val.toString());
            } else if (type.getTypeName().equals(Date.class.getTypeName())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

                try {
                    return sdf.parse(val.toString());
                } catch (ParseException var5) {
                    var5.printStackTrace();
                    return null;
                }
            } else {
                return val;
            }
        } else {
            String v = val.toString();
            if (v.toString().indexOf(".") > -1) {
                v = v.substring(0, v.toString().indexOf("."));
            }

            return new Integer(v.toString());
        }
    }
    @Override
    protected <T> List<T> loadDataList(String fieldKeys, Class type, String json) throws InstantiationException, IllegalAccessException, InvocationTargetException {

        List<T> rets = new ArrayList();
        Gson gson = new Gson();
        List rows = (List)gson.fromJson(json, rets.getClass());
        Method[] pes = type.getMethods();
        String[] fields = fieldKeys.split(",");

        //格式化一下属性
        for(int i =0;i<fields.length;i++){
            fields[i] = fields[i].replace(".","");
        }
        Method[] setPes = this.getMethodsByFields(pes, "set", fields);
        Method[] getPes = this.getMethodsByFields(pes, "get", fields);
        Iterator var12 = rows.iterator();

        while(var12.hasNext()) {
            List darray = (List)var12.next();
            T ret = (T) type.newInstance();

            for(int i = 0; i < fields.length; ++i) {
                if (setPes[i] != null && getPes[i] != null) {
                    Object v = this.convertToDest(getPes[i].getReturnType(), darray.get(i));
                    if (v != null) {
                        setPes[i].invoke(ret, v);
                    }
                }
            }
            rets.add(ret);
        }

        return rets;
    }
}
